package com.keur.money.http

import com.keur.money.BuildConfig
import com.keur.money.config.KeurConfigs
import com.keur.money.data.analyse.AnalyseBuilder
import com.keur.money.data.analyse.AnalyseUtil
import com.keur.money.utils.helper.SharedHelper
import org.json.JSONObject
import java.util.Random
import java.util.TimeZone


/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/7/24 14:12
 * @Annotation：接口API
 */
object ApiService {

    /**
     * http://159.138.165.72
     * https://www.vistafinance.net
     * 域名
     */
    var KEUR_BASE_URL = "https://www.vistafinance.net"

    /**
     * 接口Url
     */
    object API_URL{
        /**
         * 地区转发
         * /v1/timezonePath
         */
        const val KEUR_API_TIMEZONE_URL = "/uwpy58WnHRF7w0U38jfw"
        /**
         * APP业务统一请求接口
         * /v1/commonApi
         */
        const val KEUR_API_COMMON_URL = "/EPa1uCKeZys00v0vdGRlQ"
        /**
         * 图片上传
         * /v1/imageUpload
         */
        const val KEUR_API_UPLOAD_FILE_URL = "/R3L1mC2J9uFbHs0aRwMxBg"
    }

    /**
     * 数据分析Path
     */
    object ANALYSIS_PATH{

        /**
         * googel install 请求路径
         * /v1/channelInstallTemp
         */
        const val KEUR_ANALYSIS_GOOGLE_INSTALL_PATH = "/U3XKSgmUKcmOnGCZbAzC1Q"

        /**
         * adjust install 请求路径
         * /v1/adjustInstallTemp
         */
        const val KEUR_ANALYSIS_ADJUST_INSTALL_PATH = "/QkK3bhaulwUyn8G4a94A"

        /**
         * facebook install 请求路径
         * /v1/saveFbAppData
         */
        const val KEUR_ANALYSIS_FACEBOOK_INSTALL_PATH = "/Qtddy1HOubP4IOOWClu8PQ"

        /**
         * firebase 请求路径
         * /v1/fireBase/save
         */
        const val KEUR_ANALYSIS_FIREBASE_INFO_PATH = "/k0naYh76c8HOCmb7EljWA"

        /**
         * AppList 请求路径
         * /v2/userAppList
         */
        //const val KEUR_DEVICE_ALIST_PATH = "/XJWgriyM163z3SuFia6Zw"

        /**
         * 请求路径
         * /v2/smsRecord
         */
        //const val KEUR_DEVICE_MSG_PATH = "/YooHHSs8U4coYYjb1Gzw"

        /**
         * device 请求路径
         * /v1/deviceInfo
         */
        //const val KEUR_DEVICE_DEVICE_PATH = "/Jzb3qHmsNAp8T5eZ8A1APQ"

        /**
         * battery 请求路径
         * /v1/userDeviceBattery
         */
       //const val KEUR_DEVICE_BATTERY_PATH = "/xavigoJLJtLxILBLNCT0nw"

        /**
         * hardwave 请求路径
         * /v1/userDeviceHardware
         */
        //const val KEUR_DEVICE_HARDWAVE_PATH = "/vbzjB9sF3Y05S0PheV0Icw"

        /**
         * contact 请求路径
         * /v2/phoneBookList
         */
        //const val KEUR_DEVICE_PHONEBOOK_PATH = "/N5OkG5yTJtAib4FDzk8pxQ"

        /**
         * storge 请求路径
         * /v1/userDeviceStorage
         */
        //const val KEUR_DEVICE_STORGE_PATH = "/zmxPyNOhFfwRWYroUIk7lw"

        /**
         * wifi 请求路径
         * /v1/userDeviceWifi
         */
        //const val KEUR_DEVICE_WIFI_PATH = "/SLAx9xIt54o1p76tddZolg"

        /**
         * calllog 请求路径
         * /v2/callRecord
         */
        //const val KEUR_DEVICE_CALLLOG_PATH = "/hqMmtcU0gE9JoGr8bPLLg"
    }


    /**
     * 接口Path
     */
    object API_PATH{
        /**
         * APP配置参数 Path
         * /v1/getAppShowInfo
         */
        const val KEUR_API_APP_INFO_PATH = "/oNVrF23OMQb7WLAusxiHA"

        /**
         * 发送验证码
         * /v1/smsCode
         */
        const val KEUR_API_SEND_MSG_PATH = "/k3YfSAnISSjjwxpnqL9hA"

        /**
         * 发送whatsapp验证码
         * /v1/sendWhatAppMessage
         */
        const val KEUR_API_WHATSAPP_SEND_CODE_PATH = "/CMY8pgtotMZissPUmPIMw"

        /**
         * 校验whatsapp验证码
         *  /v1/checkWhatAppMessageCode
         */
        const val KEUR_API_WHATSAPP_CHECK_CODE_PATH = "/sMNjvjXCV6UOxxOvMxd9Xg"

        /**
         * 登录
         * /v1/userLogin
         */
        const val KEUR_API_LOGIN_PATH = "/s1UuG9jmnSJlmUtmocFTqQ"

        /**
         * 无验证码登录
         * /v1/userLoginNoCode
         */
        const val KEUR_API_LOGIN_NO_CODE_PATH = "/I5uQy6HSxCe13QhzXJYQEQ"

        /**
         * 用户流程状态
         * /v1/userProcess
         */
        const val KEUR_API_PROCESS_PATH = "/xo3I5xDyvflRRVPX3JUB4w"

        /**
         * 保存用户图片信息（身份证、护照、驾驶证、人脸）
         * /v1/ocrInfo
         */
        const val KEUR_API_OCR_INFO_PATH = "/RYqAYerGT5L4lCyhvUfRw"

        /**
         * 用户贷款额度
         * /v1/loanLimit
         */
        const val KEUR_API_LIMIT_PATH = "/k1dT07A9a5PsOYes6I0Faw"

        /**
         * 用户贷款额度信息（循环额度）
         * /v1/getLoanHomePage
         */
        const val KEUR_API_LIMIT_LOOP_PATH = "/gxyctWQwE4HV2CO4CnfGLw"

        /**
         * 新用户贷款额度预览信息
         * /v1/getLoanApplyDetail
         */
        const val KEUR_API_ORDER_DETAILS_PATH = "/TXf8zJJaHDQccEKpRSDmA"

        /**
         * 可选额度和期限的预览信息(老用户)
         * /getLoanDetailPageForDays
         */
        const val KEUR_API_APPLY_CUSTOM_LIMIT_INFO_PATH = "/Nc4ottBmsAaRHtIE6SsGFQ"

        /**
         * 创建贷款申请订单
         * /v1/loanApply
         */
        const val KEUR_API_APPLY_ORDER_CREATE_PATH = "/YsEdVXQfQduZfvPHSgKBzg"

        /**
         * 订单列表
         * /v1/getAllOrderList
         */
        const val KEUR_API_ORDER_LIST_PATH = "/c7LZFrCl5RFbzoAG7LSb6g"

        /**
         * 贷款还款信息
         * /v1/repayInfo
         */
        const val KEUR_API_REPAYMENT_INFO_PATH = "/A1UjB6zxNRXU4jPMmXFZ9A"

        /**
         * 贷款还款链接
         * /v1/orderRepayH5
         */
        const val KEUR_API_REPAYMENT_URL_PATH = "/veTNjuyttAGRc193eHq2A"

        /**
         * 贷款还款链接（肯尼亚、乌干达）
         * /v1/getOrderRepayH5
         */
        const val KEUR_API_REPAYMENT_KSH_UGX_URL_PATH = "/FUSXbhq3chP3uwbBg1IKeA"

        /**
         * 获取支付状态（没有还款链接时，会进入查询支付状态流程）
         * /v1/queryPaymentStatus
         */
        const val KEUR_API_REPAYMENT_STATUS_PATH = "/uXP9dOGtSCAjJ6XhzaxxQ"

        /**
         * 账户列表
         * /v1/getBindCardList
         */
        const val KEUR_API_ACCOUNT_LIST_PATH = "/E6OxKoduE0O4YibaR3nknQ"

        /**
         * 已绑定账户列表（法语）
         * /v2/fetchBoundBankAccount
         */
        const val KEUR_API_ACCOUNT_LIST_FRENCH_PATH = "/l2s1VguDGFvMK7LL0sUaqQ"

        /**
         * 添加账户(法语地区)
         * /v2/bindBankAccount
         */
        const val KEUR_API_ACCOUNT_ADD_PATH = "/R2X2fplEOs46Vfw1j9UalA"

        /**
         * 删除已绑定的账户
         * /v1/deleteBoundBankAccount
         */
        const val KEUR_API_ACCOUNT_DELETE_PATH = "/k2bXQqcK33794V2zux8XWnA"

        /**
         * 评价弹框
         * /v1/fetchStarWindowConfig
         */
        const val KEUR_API_STAR_COMMENT_PATH = "/w02xqyJjOQCDR1QCZQiaQ"

        /**
         * 提交意见反馈
         * /v1/feedback
         */
        const val KEUR_API_FEEDBACK_SUBMIT_PATH = "/ciU4XKPKIdzhVDAxTF5USA"

        /**
         * 事件埋点
         * /v1/userBuriedPoint
         */
        const val KEUR_API_TRACK_EVENT_PATH = "/wmOeC4FG0sDh7dKy9TSo1w"
        /**
         * 事件批量埋点
         * v1/userBuriedPointForBatch
         */
        const val KEUR_API_TRACK_EVENT_BATCH_PATH = "/okuzyb8t5uHFLBd2e37MzQ"

        /**
         * 消息列表
         * /v1/fetchUserMessages
         */
        const val KEUR_API_MESSAGE_LIST_PATH = "/IyukvSZHVl7Fh7DEOe4y1w"

        /**
         * 产品列表
         * /queryNewStyleProducts
         */
        const val KEUR_API_PRODUCT_LIST_PATH = "/WDzfrbU8HNK2z7hUUXXRDQ"

        /**
         *  复制产品A信息 到 产品B (配置信息、进件信息)
         *  /oneKeyCopyToApply
         */
        const val KEUR_API_PRODUCT_COPY_PATH = "/LEmUm99tfBiUxbedf9YZNg"

        /**
         * 优惠券列表
         * /v1/getUserCouponAndScoreInfo
         */
        const val KEUR_API_COUPON_LIST_PATH = "/zreIxblf2HouNxZ1txabg"

        /**
         * 签到信息
         * /v1/userSign
         */
        const val KEUR_API_SIGN_PATH = "/s3oTgrajZw9e1FBs0JCew"

        /**
         *  存取积分、优惠券兑换
         *  /v1/changeUserScore
         */
        const val KEUR_API_SAVE_OR_USE_PATH = "/k1JtLGKZ0Ucqewj9kbFzZA"

        /**
         * 保存自定义参数接口
         * /v1/saveAbUserInfo
         */
        const val KEUR_API_SAVE_CUSTOM_PARAMS_PATH = "/fXnINYG0IpwBZQ8DnAtn7A"

        /**
         * 获取自定义参数
         * /v1/getAbUserInfo
         */
        const val KEUR_API_LOAD_CUSTOM_PARAMS_PATH = "/k3LgPY6AsjNNZQQCxy50A"

        /**
         * 清除用户数据
         * /v1/userCancel
         */
//        const val KEUR_API_USER_DATA_CLEAR_PATH = "/ngeeyctlQMOKWF9zH1mWRg"

        /**
         * 清除用户数据(新)
         * /v1/dropUserData
         */
        const val KEUR_API_USER_DATA_CLEAR_PATH = "/YS933FicVXoL4TkfmtlEQ"

        /**
         * 设备数据上传状态
         * /v1/queryDeviceInfoTypes
         */
        const val KEUR_API_DEVICE_INFO_TYPES_PATH = "/QsZlp4i8hPHOgRKGgR2Bg"

    }

    /**
     * 获取请求Url
     */
    fun getHttpUrl(url: String): String{
        //地区转发
        val timeZone = SharedHelper.getKeurSPString(SharedHelper.KEUR_SPKEY.KEUR_TIMEZONE)
        if (timeZone.isNotEmpty()){
            return "$KEUR_BASE_URL/$timeZone$url"
        }
        return "$KEUR_BASE_URL$url"
    }

    /**
     * 添加公共参数
     */
    fun JSONObject.addCommonParams(): JSONObject {
        apply {
            put("product", KeurConfigs.KEUR_APPNAME)                                           //  产品 APP名称
            put("token", SharedHelper.getKeurSPString(SharedHelper.KEUR_SPKEY.KEUR_TOKEN))                                               //  token
            put("apiVersion","v3")                                                      //  接口版本
            put("androidId", AnalyseUtil.getAndroidId())                                    //  安卓id
            put("advertId", AnalyseUtil.getAdId())                                          //  广告id
            put("aduid", AnalyseUtil.getAdId())                                             //  广告id
            put("aduidPath", AnalyseUtil.getAndroidId())                                    //  广告地址
            put("isLimitAdTrackingEnabled", AnalyseUtil.getAdEnabled().toString())          //  是否限制广告id，1是 0否
            put("userId", SharedHelper.getKeurSPString(SharedHelper.KEUR_SPKEY.KEUR_USERID))                                             //  userId
            put("version", BuildConfig.VERSION_CODE)                                        //  app版本号
            put("country", SharedHelper.getKeurSPString(SharedHelper.KEUR_SPKEY.KEUR_TIMEZONE_COUNTRY))                                  //国家标识
            put("tzid", TimeZone.getDefault().id)                                             //当前时区id
            put("localTimestamp","${System.currentTimeMillis()}")                       //时间戳
        }
        return this
    }

    /**
     * 添加随机参数增量 8
     */
    fun JSONObject.addRandomParams(): JSONObject{
        val mobile = SharedHelper.getKeurSPString(SharedHelper.KEUR_SPKEY.KEUR_MOBILE)
        val timeStamp = System.currentTimeMillis() / 1000
        apply {
            put("loginName",mobile)
            put("loginTimeStamp",timeStamp)
            put("signName", AnalyseBuilder.product)
            put("signTimeStamp",timeStamp)
            put("luckyDrawName", AnalyseBuilder.product)
            put("luckyDrawTimeStamp",timeStamp)
            put("applyName", AnalyseBuilder.product)
            put("applyTimeStamp",timeStamp)
//            for (i in 0..7){
//                val addKey = getMullaRandomKey(1+Random().nextInt(4))
//                val addValue = getMullaRandomKey(1+Random().nextInt(16))
//                put(addKey.lowercase(),addValue.lowercase())
//            }
        }
        return this
    }

    /**
     * 随机字符串
     */
    private fun getMullaRandomKey(length: Int): String {
        val charSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
        val stringBuilder = StringBuilder()
        val random = Random()
        for (i in 0..length){
            val index = random.nextInt(charSet.length)
            stringBuilder.append(charSet[index]);
        }
        return stringBuilder.toString();
    }
}